package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Resource
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Resource
    private ApartmentLabelMapper apartmentLabelMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //判断是否为空
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        //新增信息
        this.saveOrUpdate(apartmentSubmitVo);

        Long apartmentId = apartmentSubmitVo.getId();

        if (isUpdate) {
            //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //删除配置信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //删除标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //插入图片信息
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)) {
                ArrayList<GraphInfo> list = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    BeanUtils.copyProperties(graphVo, graphInfo);
                    graphInfo.setItemId(apartmentId);
                    graphInfo.setItemType(ItemType.APARTMENT);
                    list.add(graphInfo);
                }
                graphInfoService.saveBatch(list);
            }

            //插入配置信息
            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)) {
                ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
                    apartmentFacilities.add(build);
                }
                apartmentFacilityService.saveBatch(apartmentFacilities);
            }

            //插入杂费信息
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            if (!CollectionUtils.isEmpty(feeValueIds)) {
                ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
                for (Long feeValueId : feeValueIds) {
                    ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
                    apartmentFeeValues.add(build);
                }
                apartmentFeeValueService.saveBatch(apartmentFeeValues);
            }

            //插入标签信息
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            if (!CollectionUtils.isEmpty(labelIds)) {
                ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
                for (Long labelId : labelIds) {
                    ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build();
                    apartmentLabels.add(build);
                }
                apartmentLabelService.saveBatch(apartmentLabels);
            }
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo==null){
            return null;
        }

        //查询图形
        List<GraphVo> graphInfoList=graphInfoMapper.selectByIdgraphInfo(ItemType.APARTMENT,id);

        //查询配置
        List<FacilityInfo> apartmentFacilityList=apartmentFacilityMapper.selectapartmentFacility(id);

        //查询标签
        List<LabelInfo> apartmentLabelList=apartmentLabelMapper.selectApartmentLabel(id);
        //查询杂费
        List<FeeValueVo> apartmentFeeValueList = apartmentFeeValueMapper.selectApartmentFeeValue(id);

        //属性对拷
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setFacilityInfoList(apartmentFacilityList);
        apartmentDetailVo.setFeeValueVoList(apartmentFeeValueList);
        apartmentDetailVo.setLabelInfoList(apartmentLabelList);

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        //判断是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //删除配置信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //删除标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
    }

}




